# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymongo


class GupiaoPipeline_mongo(object):
    def __init__(self):
        myclient = pymongo.MongoClient('127.0.0.1', 27017)
        mydb = myclient['深圳交易所']
        if '股票列表' in mydb.list_collection_names():
            mydb.drop_collection('股票列表')
        self.mycol = mydb['股票列表']
        self._id = 1

    def process_item(self, item, spider):
        item['_id'] = self._id
        self.mycol.insert_one(dict(item))
        self._id += 1
        return item


class GupiaoPipeline_txt(object):
    def open_spider(self, spider):
        self.f = open('股票列表.txt', 'a+', encoding='utf-8')

    def process_item(self, item, spider):
        self.f.write(str(item) + '\n')
        return item

    def close_spider(self, spider):
        self.f.close()
